﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.API.WeiXin;
using Newtonsoft.Json;

namespace He.Web.manage.weixin
{
    public partial class menu_list : Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                chkLoginLevel("viewWxmenu");
                ShowInfo();
            }
        }

        #region 赋值操作
        private void ShowInfo()
        {
            rptList.DataSource = new BLL.wx_menu().GetList(0);
            rptList.DataBind();
        }
        #endregion

        #region 子菜单绑定
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                HiddenField hidPid = (HiddenField)e.Item.FindControl("hidPid");
                string LitStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";
                string LitImg1 = "<img src=../images/folder_open.gif align=absmiddle />";
                string LitImg2 = "<img src=../images/t.gif align=absmiddle />";

                int pid = Convert.ToInt32(hidPid.Value);
                if (pid == 0)
                {
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, 36, LitImg2, LitImg1);
                }
            }
        }
        #endregion

        #region 获取二级菜单
        private List<Buttons.Button.SubButton> GetSubButtons(int pid)
        {
            BLL.wx_menu bll = new BLL.wx_menu();
            List<Buttons.Button.SubButton> sublst = new List<Buttons.Button.SubButton>();
            List<Model.wx_menu> lst = bll.DataTableToList(bll.GetList(5, "pid=" + pid + " and is_lock=0").Tables[0]);
            if (lst.Count > 0)
            {
                foreach (Model.wx_menu item in lst)
                {
                    Buttons.Button.SubButton subbtn = new Buttons.Button.SubButton();
                    subbtn.type = item.type;
                    subbtn.name = item.name;
                    if (item.type == API.WeiXin.Enums.MenuType.click.ToString())
                    {
                        subbtn.key = item.key;
                    }
                    else
                    {
                        subbtn.url = MenuHandle.HandleUrl(item.url, SiteDomain);
                    }
                    sublst.Add(subbtn);
                }
            }
            return sublst;
        }
        #endregion

        //删除
        protected void Del_Command(object sender, CommandEventArgs e)
        {
            chkLoginLevel("delWxmenu");

            int id = Convert.ToInt32(e.CommandArgument);
            BLL.wx_menu bll = new BLL.wx_menu();
            Model.wx_menu model = bll.GetModel(id);
            bll.DeleteAll(id);
            //保存日志
            SaveLogs(string.Format("[自定义菜单管理]删除菜单:{0}", model.name));
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editVxmenu");

            BLL.wx_menu bll = new BLL.wx_menu();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs("[自定义菜单管理]批量保存排序");
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }

        //生成菜单
        protected void lbtnGenerate_Click(object sender, EventArgs e)
        {
            BLL.wx_menu bll = new BLL.wx_menu();

            List<Buttons.Button> lst = new List<Buttons.Button>();
            List<Model.wx_menu> menuList = bll.DataTableToList(bll.GetList(3, "pid=0 and is_lock=0").Tables[0]);

            foreach (Model.wx_menu item in menuList)
            {
                bool existschild = bll.ExistsChild(item.id);//是否存在子菜单
                Buttons.Button btn = new Buttons.Button();

                if (!existschild)
                {
                    btn.type = item.type;
                }
                btn.name = item.name;
                if (!existschild)
                {
                    if (!string.IsNullOrEmpty(item.url))
                    {
                        btn.url = MenuHandle.HandleUrl(item.url, SiteDomain);
                    }
                    else
                    {
                        btn.key = item.key;
                    }
                }

                if (existschild)
                {
                    btn.sub_button = GetSubButtons(item.id);
                }

                lst.Add(btn);
            }
            Buttons btns = new Buttons
            {
                button = lst
            };

            JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore };
            string json = JsonConvert.SerializeObject(btns, settings);
            string msg = MenuHandle.ResponseMenu(json);
            if (msg == "ok")
            {
                SaveLogs("[自定义菜单管理]生成自定义菜单");
                JscriptPrint("菜单生成成功啦！", Request.RawUrl, "Success");
            }
            else
            {
                ShowError(msg);
            }
        }
    }
}